﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLDC.Common.BaseUser;
using ZLDC.Common.Estate;
using System.Data;
using System.Configuration;

namespace ZLDC.Estate
{
    public partial class Login : System.Web.UI.Page
    {
        Param info;
        protected void Page_Load(object sender, EventArgs e)
        {            

            if (!IsPostBack)
            {
                CookieHelper.DeleteUser();
                if (Request.QueryString["code"] != null && Request["name"] != null)
                {
                    CheckUsr();
                }
            }
        }

        protected void CheckUsr()
        {
            string userName = null;

            Application.Lock();
            List<string[]> codeList = Application["code"] as List<string[]>;
            Application.UnLock();
            //RegisterStartupScript("", "<script>alert(" + Request.QueryString["code"]+");</script>");
            //RegisterStartupScript("", "<script>alert(" + Request.QueryString["name"] + ");</script>");
            //RegisterStartupScript("", "<script>alert(" + codeList.Count + ");</script>");
            //RegisterStartupScript("", "<script>alert(" + codeList[0][0] + ");</script>");
            foreach (string[] code in codeList)
            {
                if (code[0] == Request.QueryString["code"] && code[1] == Request["name"])
                {
                    userName = Request["name"];
                    codeList.Remove(code);
                    Application.Lock();
                    Application["code"] = codeList;
                    Application.UnLock();
                    break;
                }
            }

            if (userName != null)
            {
                ZLDC.Common.BaseUser.UserInfo.GetInfo(userName, ref info);
                if (info.m_sFgs != null)
                {
                    CookieHelper.AddUser(this.info.m_sUserID, this.info.m_sUserName, this.info.m_sFgs);
                    Response.Redirect("Page/Index.aspx");
                    //Response.Redirect("UserControl/HousingControlSelect.aspx");
                    
                }
                else
                {
                    Response.Redirect("Login.aspx");
                    
                }
            }
            else
            {
                Response.Redirect("Login.aspx");
               
            }
        }

        /// <summary>
        /// 提交登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            string name = tbUserName.Value.Trim();
            string pass = Password1.Value.Trim();
            
            if (name != null && name != "" && pass != null && pass != "")
            {
                //DataSet ds = ZLDC.DAL.Estate.UserDAL.GetUserInfoByNo(id);
                //if (ds != null)
                //{
                //    if (ds.Tables.Count > 0)
                //    {
                //        if (ds.Tables[0].Rows.Count > 0)
                //        {
                //            if (ds.Tables[0].Rows[0]["dltflag"].ToString() == "True")
                //            {
                //                _Utility.GoError("当前用户已被禁用,请联系相关分行经理或者系统管理员!");
                //            }
                //        }
                //    }
                //}

                ZLDC.Common.BaseUser.C_UserLogin user = new ZLDC.Common.BaseUser.C_UserLogin();
                string fgs = ZLDC.Topo.BLL.Estate.UserBLL.Login(name, pass, ref this.info);
                
                if (!string.IsNullOrEmpty(fgs))
                {
                    CookieHelper.AddUser(this.info.m_sUserID, this.info.m_sUserName, this.info.m_sFgs);
                    HttpContext.Current.Session.Add("info", this.info);
                    string password = ConfigurationManager.AppSettings["password"].ToString();//通用密码不记录登录日志
                    if (!pass.Equals(password))
                    {
                        ZLDC.Topo.BLL.Estate.OperationlogBLL.p_operationlog_insert("t_user", 0, 1, "系统登录");//添加登录日志
                    }
                    Response.Redirect("Page/Index.aspx");
                    
                }
                else
                {
                    ClientScript.RegisterStartupScript(typeof(string), "", "alert('密码错误，请重新输入密码!');document.getElementById('Password1').focus()", true);
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(typeof(string), "", "alert('用户名或密码不能为空，请重新输入！');document.getElementById('tbUserName').focus()", true);
                return;
            }

            string logout = Request.Form["logout"];
            if (logout != null && logout != "")
            {
                CookieHelper.DeleteUser();
            }
        }


        /// <summary>
        /// 登录系统
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="deptName"></param>
        private void UserLogin(string name, string id, string deptName)
        {
            DataSet ds = ZLDC.DAL.Estate.UserDAL.GetUserInfoByNo(id);
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (ds.Tables[0].Rows[0]["dltflag"].ToString() == "True")
                        {
                            _Utility.GoError("当前用户已被禁用,请联系相关分行经理或者系统管理员!");
                        }
                    }
                }
            }
            ZLDC.Common.BaseUser.C_UserLogin user = new ZLDC.Common.BaseUser.C_UserLogin();
            string fgs = ZLDC.Topo.BLL.Estate.UserBLL.Login(id, name, deptName, ref this.info);

            if (!string.IsNullOrEmpty(fgs))
            {
                CookieHelper.AddUser(this.info.m_sUserID, this.info.m_sUserName, this.info.m_sFgs);
                HttpContext.Current.Session.Add("info", this.info);
               
               
                ZLDC.Topo.BLL.Estate.OperationlogBLL.p_operationlog_insert("t_user", 0, 1, "系统登录");//添加登录日志
               
                Response.Redirect("Page/Index.aspx");

            }
            else
            {
                Session.Abandon();
                Session.Clear();

                ClientScript.RegisterStartupScript(typeof(string), "", "alert('该系统中不存在此用户!');parent.parent.location.href = \""+_Utility.GetAuthUrl()+"\";", true);
            }
        }



    }
}
